package com.qububu.db.dao.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.sql.DataSource;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.qububu.db.dao.GoodsInfoDao;
import com.qububu.emporium.goods.dto.GoodsAboutDto;
import com.qububu.emporium.goods.dto.GoodsStatus;
import com.qububu.emporium.goods.dto.GoodsAboutDto.GoodsType;
import com.qububu.emporium.goods.dto.GoodsInfoDto;
import com.qububu.utils.ParamConverter;
import com.qububu.utils.SystemUtil;

@Repository
public class GoodsInfoDaoImpl implements GoodsInfoDao {

	private JdbcTemplate jdbcTemplate;
	
	public void setDataSource(DataSource dataSource) {
		this.jdbcTemplate = new JdbcTemplate(dataSource);
	}
	
	/**
	 * @author LH
	 * @功能 管理员添加菜品
	 * @param GoodsInfoDto 菜品的信息
	 * */
	public void doAddGoodsInfo(GoodsInfoDto goodsInfo){
		
		String sql = "insert into goods_info(goods_name, goods_use_num, goods_promise, goods_pic_url, goods_point, goods_price, goods_summary_pic_urls, goods_details_url, goods_type, goods_status) values(?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
		Object[] goodsInfoValues = {
				goodsInfo.getGoodsName(), goodsInfo.getGoodsUseNum(),
				goodsInfo.getGoodsPromise(), goodsInfo.getGoodsPicUrl(),
				goodsInfo.getGoodsPoint(), goodsInfo.getGoodsPrice(),
				goodsInfo.getGoodsSummaryPicUrls(), goodsInfo.getGoodsDetailsUrl(),
				goodsInfo.getGoodsType().toIntValue(),
				goodsInfo.getGoodsStatus().toIntValue()
		};
		
		jdbcTemplate.update(sql, goodsInfoValues);
	}
	
	/**
	 * @author ZCW
	 * @return 商品概况的列表
	 * */
	public List<GoodsAboutDto> getGoodsListByType(int goodsType) {
		
		List<GoodsAboutDto> goodsList = null;
		
		String sql = "SELECT goods_id, goods_name, goods_use_num, goods_pic_url, goods_price, goods_type FROM goods_info WHERE goods_type = ? AND goods_status <> ? ORDER BY goods_id DESC";
		
		List<Map<String, Object>> elemList = jdbcTemplate.queryForList(sql, new Object[]{goodsType, GoodsStatus.DELETED.toIntValue()});
		
		goodsList = new ArrayList<GoodsAboutDto>();
		GoodsAboutDto goodsAbout = null;
		for (Map<String, Object> elem : elemList) {
			goodsAbout = new GoodsAboutDto((Long) elem.get("goods_id") );
			goodsAbout.setGoodsName((String) elem.get("goods_name") );
			goodsAbout.setGoodsUseNum(Integer.parseInt(String.valueOf(elem.get("goods_use_num"))));
			goodsAbout.setGoodsPicUrl((String) elem.get("goods_pic_url") );
			goodsAbout.setGoodsPrice((String) elem.get("goods_price") );
			goodsAbout.setGoodsType(GoodsType.getType(Integer.parseInt(String.valueOf(elem.get("goods_type")))));
			
			goodsList.add(goodsAbout);
		}
		
		return goodsList;
	}
	
	/**
	 * @功能 管理员修改菜品
	 * @author LH
	 * @param GoodsInfoDto 修改过后的菜品信息
	 */
	public boolean doUpdateGoods(GoodsInfoDto goodsInfo) {
		//解密
		String goodsId = SystemUtil.decrypt(goodsInfo.getGoodsId());

		String sql = "update goods_info set goods_name=?, goods_use_num=?, goods_promise=?, goods_pic_url=?, goods_point=?, goods_price=?, goods_summary_pic_urls=?, goods_details_url=?, goods_type=?  where goods_id=?";
		Object[] goodsValues = {
				goodsInfo.getGoodsName(), goodsInfo.getGoodsUseNum(), goodsInfo.getGoodsPromise(),
				goodsInfo.getGoodsPicUrl(), goodsInfo.getGoodsPoint(), goodsInfo.getGoodsPrice(),
				goodsInfo.getGoodsSummaryPicUrls(), goodsInfo.getGoodsDetailsUrl(),
				goodsInfo.getGoodsType().toIntValue(),
				goodsId
		};
		
		jdbcTemplate.update(sql, goodsValues);
		
		return true;
	}
	
	/**
	 * @功能 管理员删除菜品
	 * @author LH
	 * @param goodsId 菜品id
	 */
	public boolean doDeleteGoods(String goodsId) {
		//解密
		goodsId = SystemUtil.decrypt(goodsId);
		
		String sql = "delete from goods_info where goods_id=?";
		
		jdbcTemplate.update(sql, Integer.parseInt(goodsId));
		
		return true;
	}
	
	/**
	 * @author ZCW
	 * @功能 根据商品id查出商品详情
	 * @param int goodsId 商品id
	 * @return GoodsInfoDto 商品详情
	 * */
	public GoodsInfoDto getGoodsInfoById(String goodsId) {
		
		int reGoodsId = ParamConverter.convertToInteger(SystemUtil.decrypt(goodsId));
		String sql = "select * from goods_info where goods_id = ?";
		Map<String, Object> map = jdbcTemplate.queryForMap(sql, reGoodsId);

		GoodsInfoDto goodsInfo = new GoodsInfoDto((Long) map.get("goods_id") );
		
		goodsInfo.setGoodsType(GoodsType.getType((Integer) map.get("goods_type") ) );
		goodsInfo.setGoodsDetailsUrl(String.valueOf(map.get("goods_details_url") ) );
		goodsInfo.setGoodsName(String.valueOf(map.get("goods_name")));
		goodsInfo.setGoodsPicUrl(String.valueOf(map.get("goods_pic_url")));
		goodsInfo.setGoodsPoint(ParamConverter.convertToInteger(String.valueOf(map.get(("goods_point")))));
		goodsInfo.setGoodsPrice(String.valueOf(map.get("goods_price")));
		goodsInfo.setGoodsPromise(String.valueOf(map.get("goods_promise")));
		goodsInfo.setGoodsSummaryPicUrls(String.valueOf(map.get("goods_summary_picUrls")));
		goodsInfo.setGoodsUseNum(ParamConverter.convertToInteger(String.valueOf(map.get(("goods_use_num")))));
		
		return goodsInfo;
	}

	public void doChangeGoodsStatus(String goodsId, int goodsStatus) {
		String sql = "UPDATE goods_info SET goods_status=? WHERE goods_id=?";
		Object[] data = {
				goodsStatus,
				ParamConverter.convertToInteger(SystemUtil.decrypt(goodsId))
		};
		jdbcTemplate.update(sql, data);
	}
}
